package cn.cjlmonster.demo02.ui;

import android.Manifest;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.util.Log;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;

import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.GlideEngine;
import com.zhihu.matisse.filter.Filter;

import java.util.List;

import cn.cjlmonster.demo02.R;
import cn.cjlmonster.demo02.base.BaseAppCompatActivity;
import cn.cjlmonster.demo02.utils.GifSizeFilter;

/**
 * cn.cjlmonster.demo02.ui
 *
 * @author cjl
 * @project Demo02
 * @date 2022/11/17 22:47
 */
public class ImagePickerActivity extends BaseAppCompatActivity {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_image_picker;
    }

    @Override
    protected boolean canBack() {
        return true;
    }

    private ActivityResultLauncher<String[]> launcher;


    @Override
    protected void initView() {
        launcher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), result -> {

        });
        findViewById(R.id.btn_get_perm).setOnClickListener(v -> {
            launcher.launch(new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            });
        });
        findViewById(R.id.btn_pick_image).setOnClickListener(v -> pickImage());
    }

    private void pickImage() {
        Matisse.from(this)
                .choose(MimeType.ofImage())
                .countable(false)
                .maxSelectable(1)
                .addFilter(new GifSizeFilter(320, 320, 5 * Filter.K * Filter.K))
                .gridExpectedSize(getResources().getDimensionPixelSize(R.dimen.grid_expected_size))
                .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
                .thumbnailScale(0.85f)
                .theme(com.zhihu.matisse.R.style.Matisse_Dracula)
                .imageEngine(new GlideEngine())
                .showPreview(true) // Default is `true`
                .forResult(100);
    }

    List<Uri> mSelected;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {
            mSelected = Matisse.obtainResult(data);
            Log.d("Matisse", "mSelected: " + mSelected);
        }
    }
}
